package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Country;

@Repository(value = "countryDao")
@Transactional(readOnly = false)
public class CountryDaoImpl implements CountryDao {

	private EntityManager em = null;	
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	@SuppressWarnings("unchecked")
	public List<Country> getCountryList() {
		return em.createNamedQuery("Country.findAll").getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Country> getCountryList(Short idCountry){
		Query  q = em.createNamedQuery("Country.findByActive");
    	q.setParameter("countryId",idCountry);
    	return q.getResultList();	
	}
	@SuppressWarnings("unchecked")
	public List<Country> getCountryListWithIDCountry(Short idCountry){
		Query  q = em.createNamedQuery("Country.findWithIdCountryByActive");
    	//q.setParameter("active",active);
    	q.setParameter("countryId",idCountry);
    	return q.getResultList();	
	}
	@SuppressWarnings("unchecked")
	public List<Country> getCountryListExcuseIDCountry(Short idCountry,boolean active){
		Query  q = em.createNamedQuery("Country.findExcuseIdCountryByActive");
    	q.setParameter("active",active);
    	q.setParameter("countryId",idCountry);
    	return q.getResultList();	
	}
	
	
	public Country findCountryByID(Short idCountry) {
		Country res=null;
		try {
	    	res=em.find(Country.class,idCountry);
		} catch (Exception e) {}
    	return res;
	}
	public void remove(Country country) {
		em.remove(em.getReference(Country.class, country.getCountryId()));
	}

	public void update(Country country) {
		em.merge(country);
	}
	public void add(Country country) {
		em.persist(country);
	}
}
